package com.bawei.car.domain;

import com.bawei.car.domain.contants.CarContants;
import com.bawei.car.domain.model.CarProductModel;
import com.bawei.common.core.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * 购物车实体类
 */
@Data
public class ShopCar {

    private Long id;

    /**
     * 商品id
     */
    private Long productId;
    /**
     * 商品规格id
     */
    private Long productSkuId;
    /**
     * 商品数量
     */
    private Long number;

    /**
     * 是否选中
     */
    private String isChecked;
    /**
     * 状态
     */
    private String status;

    /**
     * 创建人
     */
    private String createBy;

    /**
     * 创建时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /**
     * 修改时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public ShopCar(Long id, Long productId, Long productSkuId, Long number, String isChecked, String status) {
        this.id = id;
        this.productId = productId;
        this.productSkuId = productSkuId;
        this.number = number;
        this.isChecked = isChecked;
        this.status = status;
    }

    public ShopCar(Long id, Long productId, Long productSkuId, Long number, String isChecked, String status, String createBy, Date createTime, Date updateTime) {
        this.id = id;
        this.productId = productId;
        this.productSkuId = productSkuId;
        this.number = number;
        this.isChecked = isChecked;
        this.status = status;
        this.createBy = createBy;
        this.createTime = createTime;
        this.updateTime = updateTime;
    }

    public ShopCar() {
    }

    /**
     * 添加购物车
     * @param shopCar
     * @return
     */
    public static  ShopCar addCarInfo(ShopCar shopCar){
        return new ShopCar(null,shopCar.getProductId(),shopCar.getProductSkuId(),
                shopCar.number == null ? CarContants.CAR_SKU_DEF_NUMBER : shopCar.number,
                StringUtils.isEmpty(shopCar.isChecked) ? CarContants.CAR_SKU_DEF_CHECK : shopCar.isChecked ,
                shopCar.status=CarContants.CAR_SKU_DEF_STATUS,
                shopCar.createBy,
                new Date(),
                null);
    }

    /**
     * 购物车构建方法
     * @return
     */
    public static ShopCar buildCarInfo(ShopCar shopCar){
        return buildCarInfo(shopCar.productId,null,null,null);
    }

    public static ShopCar buildCarInfo(Long productId,Long productSkuId,
                                       Long number,String isChecked){
        return new ShopCar(null,productId,productSkuId,
                number == null ? CarContants.CAR_SKU_DEF_NUMBER : number,
                StringUtils.isEmpty(isChecked) ? CarContants.CAR_SKU_DEF_CHECK : isChecked ,
                "");
    }

    public  CarProductModel infoToModel(){
        CarProductModel carProductModel = new CarProductModel();
        carProductModel.setProductId(this.productId);
        carProductModel.setNumber(this.number);
        carProductModel.setIsChecked(this.isChecked);
        return carProductModel;
    }

    public  CarProductModel infoToModelAndIsChecked(){
        CarProductModel carProductModel = new CarProductModel();
        carProductModel.setProductId(this.productId);
        carProductModel.setNumber(this.number);
        carProductModel.setIsChecked(this.isChecked.equals("Y") ? "true" : "false");
        return carProductModel;
    }

    /**
     * 改变 redis isChecked
     * @param carProductModel
     * @return
     */
    public static String upChecked(CarProductModel carProductModel){
        String tt = null;
        if(carProductModel.getIsChecked()){
            tt = "false";
        }else {
            tt = "true";
        }
        return tt;
    }
}
